package edu.gatech.oops.servlets;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class signUp
 */
public class signUp extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public signUp() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException,
			ClassNotFoundException {
		String studentName = request.getParameter("studentName");
		String courseName = request.getParameter("courseName");
		String labName = request.getParameter("labName");
		String teamName = request.getParameter("teamName");
		String signUpType = request.getParameter("type");
		FileInputStream f_in = new FileInputStream(
				"/home/pushkara/workspace/LabAssistan/" + courseName + "_"
						+ labName + ".data");
		ObjectInputStream obj_in = new ObjectInputStream(f_in);
		Lab myLab = (Lab) obj_in.readObject();
		System.out.println(myLab.getLabName());

		Team[] teamsArray = myLab.getTeamsArray();
		if (teamsArray == null)
			System.out.println("null");
		if (signUpType.equals("create")) {
			boolean found = false;
			for (int i = 0; i < teamsArray.length; i++) {
				if (teamsArray[i].getTeamName() == null && !found) {
					teamsArray[i].setTeamName(teamName);
					teamsArray[i].add(studentName);
					found = true;
					System.out.println(teamsArray[i]);
				}
			}
			if (!found) {
				// put stuff that says that they can't create team and try again
            File eFile = new File("/home/pushkara/workspace/LabAssistan/LogExceptions.txt");
            BufferedWriter eWriter = new BufferedWriter(new FileWriter(eFile, true));
            eWriter.write(Calendar.getInstance().getTime() + ": Couldn't create team.\n");
            eWriter.close();
			}

		} else {
			boolean add = false;
			for (int i = 0; i < teamsArray.length; i++) {
				System.out.println(teamsArray[i]);
				if (teamsArray[i].getTeamName() != null) {
					if (teamsArray[i].getTeamName().equals(teamName)) {
						add = teamsArray[i].add(studentName);
					}
				}
			}
			if (add) {
				// congratulate them for beign added
				System.out.println("You made it!");
			} else {
				// tell them they're not allowed to join the team cause they're
				// a loser
            File eFile = new File("/home/pushkara/workspace/LabAssistan/LogExceptions.txt");
            BufferedWriter eWriter = new BufferedWriter(new FileWriter(eFile, true));
            eWriter.write(Calendar.getInstance().getTime() + ": Couldn't join team.\n");
            eWriter.close();
				System.out.println("You didn't make it.");
			}
		}
		myLab.setTeamsArray(teamsArray);
		FileOutputStream lab_out = new FileOutputStream(
				"/home/pushkara/workspace/LabAssistan/" + courseName + "_"
						+ labName + ".data");
		ObjectOutputStream object_out = new ObjectOutputStream(lab_out);
		object_out.writeObject(myLab);
		object_out.close();
		lab_out.close();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			processRequest(request, response);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			processRequest(request, response);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
	}

}
